package com.ns.school.controller.wave;

import com.github.pagehelper.PageHelper;
import com.ns.school.common.utils.CommonUtil;
import com.ns.school.common.utils.JsonUtil;
import com.ns.school.controller.base.BaseController;
import com.ns.school.po.edu.BookDB;
import com.ns.school.po.edu.LayuiResult;
import com.ns.school.po.wave.WaveDevicePO;
import com.ns.school.po.wave.WaveStationPO;
import com.ns.school.service.wave.WaveDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@Slf4j
@RequestMapping("device")
public class WaveDeviceController extends BaseController {

    @Autowired
    private WaveDeviceService waveDeviceService;

    @RequestMapping("/list")
    public String list(Model model) {

        return "view/device/list";
    }


    @RequestMapping("/add")
    public String add(Model model) {
        return "view/device/add";
    }

    @RequestMapping("edit")
    public String edit(Model model, Integer id, ModelMap modelMap) {
        WaveDevicePO waveDevicePO=waveDeviceService.selectDevice(id);
        modelMap.put("waveDevice",waveDevicePO);
        return "view/device/edit";
    }

    @RequestMapping("findAllDevices")
    @ResponseBody
    public Object findAllDevices(WaveDevicePO waveDevicePO, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<WaveDevicePO> listAll = waveDeviceService.findAllDevices(waveDevicePO);
        return CommonUtil.getStringObjectMap(listAll);
    }

    //添加设备
    @RequestMapping("addDevices")
    @ResponseBody
    public LayuiResult<WaveDevicePO> addDevices(@RequestBody WaveDevicePO waveDevicePO) {
        LayuiResult<WaveDevicePO> result=new LayuiResult<>();
        waveDeviceService.addDevices(waveDevicePO,result);
        return result;


    }

    //编辑设备
    @RequestMapping("editDevice")
    @ResponseBody
    public LayuiResult<WaveDevicePO> editDevice(@RequestBody WaveDevicePO waveDevicePO) {
        LayuiResult<WaveDevicePO> result=new LayuiResult<>();
        Integer b=waveDeviceService.updateDevice(waveDevicePO);
        if(b>0){
            result.setCode(0);
            result.setMsg("编辑成功");
        }
        return result;
    }

    //删除设备
    @RequestMapping(value = "updateDevice")
    @ResponseBody
    public LayuiResult<WaveDevicePO> updateDel(WaveDevicePO params) {
        LayuiResult result = new LayuiResult();
        log.info(JsonUtil.toStr(params));
        waveDeviceService.delDevice(result,params);
        return result;
    }


    //变电站批量删除
    @RequestMapping("delDeviceAll")
    @ResponseBody
    public LayuiResult<WaveDevicePO> bookUpdate(WaveDevicePO params,String ids) {
        LayuiResult result = new LayuiResult<>();
        String[] strarray = ids.split(",");
        //删除
        for (int i = 0; i < strarray.length; i++) {
            if (!strarray[i].isEmpty()) {
                result.setCode(0);
                result.setMsg("批量删除成功");
                waveDeviceService.delDeviceAll(Integer.valueOf(strarray[i]));
            } else {
                result.setCode(500);
                result.setMsg("删除失败");
            }

        }
        return result;
    }

}
